How can I create a square 10x10 grid using nested for loops in Java? [migrated]
Posted
by
help
on Programmers
See other posts from Programmers
or by help
Published on 2011-11-27T20:25:06Z
Indexed on
2011/11/28
2:02 UTC
Read the original article
Hit count: 423
java
I'm trying to create a 10x10 grid using for loops in Java. I'm able to create rows going up and down but not repeating.
for(int i = 1; i < temperatures.length; i++) {
temperatures[i] = (temperatures[i-1] + 12) / 2; //takes average of 12 and previous temp
}
}
public void paint(Graphics g) {
for(int y = 1; y < 9; y++) {
g.setColor(Color.black);
g.drawRect(10, 10, 10, 10);
g.drawRect(10, 10, 10, 20);
g.drawRect(10, 10, 10, 30);
g.drawRect(10, 10, 10, 40);
g.drawRect(10, 10, 10, 50);
g.drawRect(10, 10, 10, 60);
g.drawRect(10, 10, 10, 70);
g.drawRect(10, 10, 10, 80);
g.drawRect(10, 10, 10, 90);
g.drawRect(10, 10, 10, 100);
for(int x = 1; x < 9; x++) {
g.setColor(Color.black);
g.drawRect(10, 10, 10, 10);
g.drawRect(10, 10, 20, 10);
g.drawRect(10, 10, 30, 10);
g.drawRect(10, 10, 40, 10);
g.drawRect(10, 10, 50, 10);
g.drawRect(10, 10, 60, 10);
g.drawRect(10, 10, 70, 10);
g.drawRect(10, 10, 80, 10);
g.drawRect(10, 10, 90, 10);
g.drawRect(10, 10, 100, 10);
}
}
}
}
© Programmers or respective owner